package com.team.lostProperty.controller;

import com.google.gson.Gson;
import com.team.lostProperty.entity.LikeMap;
import com.team.lostProperty.entity.Result;
import com.team.lostProperty.entity.User;
import com.team.lostProperty.service.LikeMapService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/likeMap")
public class LikeMapController {
    private Result result = new Result();

    @Resource
    private LikeMapService likeMapService;

    @CrossOrigin
    @RequestMapping("/insert")
    public Result insertLikeMap(@RequestParam Map<String, String> likeMap) {
        String createdDate = likeMap.get("createdDate");
        long userId = Long.parseLong(likeMap.get("userId"));
        long pushInfoId = Long.parseLong(likeMap.get("pushInfoId"));
        Boolean date=likeMapService.insertLikeMap(userId, pushInfoId, createdDate);
        result.setData(date);
        result.setCount(1);
        result.setCode(Result.RESULT_OK);
        result.setMsg("点赞成功");
        return result;

    }

    // 根据id获取发布信息
    @CrossOrigin
    @RequestMapping("/selectByPushInfoId")
    private Result getPushInfoById(@RequestParam Map<String, String> idMap) {
        Long pushInfoId = Long.parseLong(idMap.get("id"));
        try {
            List<LikeMap> likeMaps = likeMapService.selectByPushInfoId(pushInfoId);
            Gson gson = new Gson();
            String date = gson.toJson(likeMaps);
            result.setData(date);
            result.setCount(likeMaps.size());
            result.setCode(Result.RESULT_OK);
            result.setMsg("添加成功");
            return result;
        } catch (Exception e) {

        }
        return result;

    }

}
